using System;
using System.Collections.Generic;
using UnityEngine;

namespace Entities
{
	
	public class MobGroup
	{
		private int sizeBase;
		private int powerBase;
		
		private bool asleep;
		
		private Vector3 startingPosition;
		
		protected HashSet<Fighter> enemies; //ensemble des ennemis, auto-attaquera à vue
		
		private System.Random random;
		
		private List<Mob> mobs;
		
		public bool spawned;
		
		
		public MobGroup (int size, int power, int seed)
		{
			this.sizeBase = size;
			this.powerBase = power;
			this.asleep = true;
			this.random = new System.Random(seed);
			enemies = new HashSet<Fighter>();
			spawned = false;
		}
		
		public void wakeUp(){
			asleep = false;	
		}
		
		public void setStartingPosition(Vector3 pos){
			startingPosition = pos;
		}
		
		public Vector3 getStartingPosition(){
			return startingPosition;
		}
		
		public void instanciateMobs<T>(int baselevel) where T : Mob, new(){
			mobs = new List<Mob>();
			T leader = new T();
			int nbLeaderMods = (int) Math.Ceiling(powerBase * 0.6); //inclure mob factor
			int modsLeft = powerBase - nbLeaderMods;
			(leader as Mob).addMods(nbLeaderMods, random);
			(leader as Mob).setLevel(baselevel+nbLeaderMods);
			(leader as Mob).setMobGroup(this);
			
			for (int i = 0 ; i < Math.Floor(sizeBase * (leader as Mob).groupSizeFactor()) ; i++){ //inclure mob factor
				T mob = new T();
				(mob as Mob).setLevel(baselevel);
				(mob as Mob).setMobGroup(this);
				mob.setLeader(leader);
				mobs.Add(mob);
			}
			
			int index = 0;
			while(modsLeft > 0 && mobs.Count != 0){ 
				T mob = mobs[index] as T;
				int nbmods = random.Next(modsLeft + 1);
				(mob as Mob).addMods(nbmods, random);
				(mob as Mob).addLevel(nbmods);
				modsLeft -= nbmods;
				index = (index + 1)%mobs.Count;
			}
			
			mobs.Add(leader);
		}
		
		public List<Mob> getMobs(){
			return mobs;	
		}
		
		public HashSet<Fighter> getEnemies(){
			return enemies;	
		}
		
		public void clearEnemies(){
			enemies.Clear();	
		}
		
		public void addEnemy(Fighter fighter){
			enemies.Add(fighter);;	
		}
	}
}

